VBAコードがセルをアクティブにしない (VBA code not activating cell)


問題の説明

VBAコードがセルをアクティブにしない (VBA code not activating cell)

コードに問題があるようです。しかし、私は問題を理解することができません。ワークブックに 2 つのタブがあります。メインシートとサブシート。メインシートのドロップダウンで「はい」を選択すると、サブシートへの入力が有効になります。メイン シートのドロップダウンで [いいえ] を選択すると、サブシートのセルが無効になります。

私の問題 : [いいえ] を選択すると、どのシートにも [アクティブ セル] が表示されません。 . アクティブ セルとは、セルをクリックしたときに表示される緑色の境界線のことです (スクリーンショットを添付)。

メイン シートのコード

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
        If Not Intersect(Target, Range("R12")) Is Nothing Then
            If Target.Value = "YES" Then 
                Call Enabler 
            Else 
                Call Disabler
            End If
        End If
    Application.EnableEvents = True
End Sub

モジュールのコード<


リファレンスソリューション

方法 1:

Something like the following should work for you...

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ExitSub
    Application.EnableEvents = False
    If Target.Address <> "$R$12" Then Exit Sub
    If Target.Value = "YES" Then
        Call LockRange(False)
    Else
        Call LockRange(True)
    End If

ExitSub:
    Application.EnableEvents = True
End Sub

Private Function LockRange(bFlag As Boolean) As Boolean
    On Error Resume Next
    With ThisWorkbook.Sheets("SubSheet")
        .Unprotect Password:="xyz"
        .Range("E13:E14").Locked = bFlag
        .Protect Password:="xyz"
        'Debug.Print bFlag
    End With
    LockRange = True
End Function

方法 2:

I guess you have to type in:

.EnableSelection = xlNoRestrictions

BTW you may want to shorten your code by merging Disabler() and Enabler() subs into one Sub:

Public Sub DisableSubSheet(disable As Boolean)
    With ThisWorkbook.Worksheets("SubSheet")
        .Unprotect Password:="xyz"
        .Range("E13:E14").Locked = disable
        .Protect Password:="xyz"
        .EnableSelection = xlNoRestrictions '<‑‑| make it possible for user to select cells
    End With
End Sub

thus, changing your Worksheet_Change event handler code as follows:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Not Intersect(Target, Range("R12")) Is Nothing Then
        If Target.Value = "YES" Then
            DisableSubSheet False '<‑‑| in place of previous 'Call Enabler'
        Else
            DisableSubSheet True '<‑‑| in place of previous 'Call Disabler'
        End If
    End If
    Application.EnableEvents = True
End Sub

(by sadyTragamoruser3598756)

リファレンスドキュメント

  1. VBA code not activating cell (CC BY‑SA 2.5/3.0/4.0)

#vba #excel






関連する質問

2010カスタムリボンにアクセス (access 2010 custom ribbon)

セルに値が保存されているファイルを開く (Open files with values stored in cells)

コンソールはプログラミング言語で制御できますか? (Can consoles be controlled by programming languages?)

このコードを変更して、最後の行の下の行にデータを貼り付けるにはどうすればよいですか? (How do I modify this code to paste in the row under last row with data?)

Selenium-vba クラス名で要素を取得 (Selenium-vba Get element by Class Name)

セルからデータを抽出し、セルをアルファベット順に並べ替えるにはどうすればよいですか? (How do I extract data from a cell and order the cells alphabetically?)

VBAコードがセルをアクティブにしない (VBA code not activating cell)

列をExcel VBAループして、空白以外をコピーして他の3つの列に貼り付けますか? (Excel VBA Loop through Column to Copy and Paste Non Blanks to 3 other Columns?)

コードは 2 つの製品で動作しますが、3 番目の製品コードを追加すると、データが取得されますが、3 番目の製品だけに保存されません。どうしたの? (Code works with two products but when I add a third product code grabs data but doesn't save it only for third product. What's wrong with it?)

シート 2 の範囲内のセル名に基づいてシート (シート 1) を複製して名前を変更するために必要な VBA コード (VBA code needed to duplicate and rename a sheet (Sheet 1) based on cell names in a range on Sheet 2)

signtool.exe エラー: Excel マクロの署名時に SignerSign() が失敗しました (-2147220492/0x800403f4) (signtool.exe Error: SignerSign() failed (-2147220492/0x800403f4) when signing Excel Macro)

Excelで1つの列の「 - 」で区切られたデータを複数に分割する (Divide data separated from ' - ' in one column into more in excel)







コメント